package user_interface;

import DataAccessObject.Connection;
import DataAccessObject.tblConDauDAO;
import DataAccessObject.tblQuanHeDAO;
import DataAccessObject.tblThanhVienDAO;
import EntityObject.Cbo;
import EntityObject.tblConDauEO;
import EntityObject.tblQuanHeEO;
import EntityObject.tblThanhVienEO;
import java.awt.HeadlessException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class ThanhVien_UI extends javax.swing.JFrame {
    public tblThanhVienEO _tblThanhVienEO = new tblThanhVienEO(0, null, null, false,null, null, null, null, null, null, null, 0 , null, null);
    public tblQuanHeEO _tblQuanHeEO = new tblQuanHeEO(0,0,0);
    public ThanhVien_UI(){
        initComponents();
        //SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        //Calendar c= dateChooserDialog1.getSelectedDate();
        //c.getTime();
        loadTable();
        LoadDataToComboBox();
    }
    
    public void loadTable() {
        DefaultTableModel tableModel = new DefaultTableModel();
        Vector title = new Vector();
        title.addElement("Mã thành viên");
        title.addElement("Tên thành viên");
        title.addElement("Tên húy");
        title.addElement("Giới tính");
        title.addElement("Nơi sinh");
        title.addElement("Nơi ở hiện tại");        
        title.addElement("Ngày sinh");
        title.addElement("Ngày mất");
        title.addElement("Nơi an táng");
        title.addElement("Trình độ văn Hóa");
        title.addElement("Nghề nghiệp");
        title.addElement("Đời thứ");
        title.addElement("Nhóm máu");
        title.addElement("Ghi chú");        
        tableModel.setColumnIdentifiers(title);
   
        _tblThanhVienEO.setPK_iMaTV(0);
        _tblThanhVienEO.setsTenTV(null);
        _tblThanhVienEO.setsTenHuy(null);
        _tblThanhVienEO.setbGioiTinh(null);
        _tblThanhVienEO.setsNoiSinh(null);
        _tblThanhVienEO.setsNoiOHienTai(null);
        _tblThanhVienEO.setsNgaySinh(null);
        _tblThanhVienEO.setsNgayMat(null);
        _tblThanhVienEO.setsNoiAnTang(null);
        _tblThanhVienEO.setsTrinhDoVH(null);
        _tblThanhVienEO.setsNgheNghiep(null);
        _tblThanhVienEO.setiDoiThu(0);
        _tblThanhVienEO.setsNhomMau(null);
        _tblThanhVienEO.setsGhiChu(null);
        String gt;
        try {
            ResultSet rs = tblThanhVienDAO.ThanhVien_SelectList(_tblThanhVienEO);
            while (rs.next()) {
                Vector data = new Vector();
                data.addElement(rs.getInt(1));  // Mã TV kiểu Int
                data.addElement(rs.getString(2));  // Tên TV kiểu string
                data.addElement(rs.getString(3));  // Mã TV kiểu String
                if(rs.getBoolean(4) == false){
                gt="nữ";
                }
                else{
                    gt="nam";
                }
                data.addElement(gt);
                data.addElement(rs.getString(5));  // Mã TV kiểu String
                data.addElement(rs.getString(6));
                data.addElement(rs.getString(7));
                data.addElement(rs.getString(8));
                data.addElement(rs.getString(9));
                data.addElement(rs.getString(10));
                data.addElement(rs.getString(11));
                data.addElement(rs.getInt(12));
                data.addElement(rs.getString(13));
                data.addElement(rs.getString(14));
                tableModel.addRow(data);
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "Lỗi Load Dữ liệu tại bảng Thành viên:' "+e.getMessage()+ " '");
        }
        tbThanhVien.setModel(tableModel);
    }
    
    public void LoadDataToComboBox(){
        String sql = "select PK_iMaTV,sTenTV from tblthanhvien where bGioiTinh = 1;";
       try{
        Statement cmd = Connection.conn().createStatement();
        ResultSet rs = cmd.executeQuery(sql);
   
        while(rs.next()){
            cbbBo.addItem(new ComboItem(rs.getString(2), rs.getInt(1)));
        }
       
     //   DefaultComboBoxModel modelCbo = new DefaultComboBoxModel(cbo);
    //    cbbBo.setModel(modelCbo);
        
        
        //Bố
//        tblThanhVienEO objtblThanhVienEO = new tblThanhVienEO(0, null, null, false,null, null, null, null, null, null, null, 0 , null, null);
//        
//        try {
//            rs = tblThanhVienDAO.ThanhVien_SelectList(objtblThanhVienEO);
//       cbbBo.removeAll();
//        while (rs.next()) {
//            cbbBo.addItem(new ComboItem(rs.getString(2), rs.getInt(1)));
//        } 
//        } catch (SQLException ex) {
//            Logger.getLogger(ThanhVien_UI.class.getName()).log(Level.SEVERE, null, ex);
//        }
        
        //mẹ
        tblConDauEO objtblConDauEO = new tblConDauEO(0, null, null, null, null, null, null , null, 0, null);
       
            rs = tblConDauDAO.ConDau_SelectList(objtblConDauEO);
       cbbMe.removeAll();
        while (rs.next()) {
            cbbMe.addItem(new ComboItem(rs.getString(2), rs.getInt(1)));
        } 
        } catch (SQLException ex) {
            Logger.getLogger(ThanhVien_UI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        dateChooserDialog1 = new datechooser.beans.DateChooserDialog();
        dateChooserDialog2 = new datechooser.beans.DateChooserDialog();
        dateChooserDialog3 = new datechooser.beans.DateChooserDialog();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbThanhVien = new javax.swing.JTable();
        txtsNoiOHienTai = new javax.swing.JTextField();
        txtPK_iMaTV = new javax.swing.JTextField();
        btnTimKiem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnThem = new javax.swing.JButton();
        txtsNoiSinh = new javax.swing.JTextField();
        txtsTenTV = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        rbtnNam = new javax.swing.JRadioButton();
        rbtnNu = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        txtsTenHuy = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtsNoiAnTang = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtsTrinhDoVH = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        txtiDoiThu = new javax.swing.JTextField();
        txtsNhomMau = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        txtsNgheNghiep = new javax.swing.JTextField();
        txtsGhiChu = new javax.swing.JTextField();
        cbbBo = new javax.swing.JComboBox();
        cbbMe = new javax.swing.JComboBox();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        txtsNgaySinh = new javax.swing.JTextField();
        txtsNgayMat = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tbThanhVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbThanhVien.setUpdateSelectionOnSort(false);
        tbThanhVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbThanhVienMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbThanhVien);

        txtsNoiOHienTai.setName(""); // NOI18N
        txtsNoiOHienTai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtsNoiOHienTaiActionPerformed(evt);
            }
        });

        txtPK_iMaTV.setEnabled(false);
        txtPK_iMaTV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPK_iMaTVActionPerformed(evt);
            }
        });

        btnTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/btnSearch.png"))); // NOI18N
        btnTimKiem.setText("Tìm kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        btnXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/btnDelete.png"))); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/btnEdit.png"))); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/btnExit.png"))); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 32)); // NOI18N
        jLabel1.setText("QUẢN LÝ THÀNH VIÊN TRONG HỌ");

        jLabel2.setText("Mã thành viên:");

        jLabel3.setText("Tên thành viên:");

        btnThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/btnAdd.png"))); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        txtsNoiSinh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtsNoiSinhActionPerformed(evt);
            }
        });

        jLabel4.setText("Nơi sinh:");

        jLabel5.setText("Nơi ở hiện tại:");

        jLabel6.setText("Ngày sinh:");

        jLabel9.setText("Giới tính:");

        buttonGroup1.add(rbtnNam);
        rbtnNam.setSelected(true);
        rbtnNam.setText("Nam");
        rbtnNam.setToolTipText("");
        rbtnNam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnNamActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbtnNu);
        rbtnNu.setText("Nữ");
        rbtnNu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnNuActionPerformed(evt);
            }
        });

        jLabel8.setText("Tên húy:");

        jLabel10.setText("Ngày mất:");

        jLabel11.setText("Nơi an táng:");

        jLabel12.setText("Trình độ VH:");

        jLabel13.setText("Nghề nghiệp:");

        jLabel14.setText("Nhóm máu:");

        jLabel15.setText("Đời thứ:");

        jLabel16.setText("Ghi chú:");

        txtsGhiChu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtsGhiChuActionPerformed(evt);
            }
        });

        cbbBo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbBoActionPerformed(evt);
            }
        });

        jLabel17.setText("Tên Bố:");

        jLabel18.setText("Tên Mẹ:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(37, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 806, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(100, 100, 100)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel11)
                                            .addComponent(jLabel12)
                                            .addComponent(jLabel16))
                                        .addGap(30, 30, 30)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(txtsTrinhDoVH, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(txtiDoiThu, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                    .addGap(65, 65, 65)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel13)
                                                        .addComponent(jLabel14))
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(txtsNhomMau, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(txtsNgheNghiep, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addComponent(txtsGhiChu, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 503, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(txtsNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                    .addComponent(jLabel10)
                                                    .addGap(26, 26, 26)
                                                    .addComponent(txtsNgayMat, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addGap(95, 95, 95)))
                                            .addComponent(txtsNoiAnTang, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel4)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(22, 22, 22)
                                        .addComponent(jLabel1))
                                    .addComponent(jLabel15)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel17)
                                            .addComponent(jLabel18)
                                            .addComponent(jLabel5))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtsNoiOHienTai, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtsNoiSinh, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cbbMe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(txtPK_iMaTV, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(txtsTenHuy, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(cbbBo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(63, 63, 63)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jLabel9)
                                                    .addComponent(jLabel3))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(txtsTenTV, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(rbtnNam)
                                                        .addGap(18, 18, 18)
                                                        .addComponent(rbtnNu))))))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(51, 51, 51)
                                .addComponent(btnTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(44, 44, 44)
                                .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(62, 62, 62)
                                .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(82, 82, 82)
                                .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(50, 50, 50)
                                .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jLabel1)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPK_iMaTV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtsTenTV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(rbtnNu)
                    .addComponent(jLabel8)
                    .addComponent(txtsTenHuy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rbtnNam))
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(cbbBo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel18)
                    .addComponent(cbbMe, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtsNoiSinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtsNoiOHienTai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtsNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel10)
                        .addComponent(txtsNgayMat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtsNoiAnTang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtsTrinhDoVH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel13)
                        .addComponent(txtsNgheNghiep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtsNhomMau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel15)
                        .addComponent(jLabel14)
                        .addComponent(txtiDoiThu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtsGhiChu, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                    .addComponent(jLabel16))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(57, 57, 57))
        );

        setSize(new java.awt.Dimension(869, 691));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        JOptionPane.showMessageDialog(this, "Chức năng này đang xây dựng");
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        _tblThanhVienEO.setPK_iMaTV(Integer.parseInt(txtPK_iMaTV.getText()));      
        _tblThanhVienEO.setsTenTV(txtsTenTV.getText());
        _tblThanhVienEO.setsTenHuy(txtsTenHuy.getText());
        if(rbtnNam.isSelected()==true){
             _tblThanhVienEO.setbGioiTinh(true);
        }
        
        if(rbtnNu.isSelected()==true){
             _tblThanhVienEO.setbGioiTinh(false);
        }
        _tblThanhVienEO.setsNoiSinh(txtsNoiSinh.getText());
        _tblThanhVienEO.setsNoiOHienTai(txtsNoiOHienTai.getText());
        _tblThanhVienEO.setsNgaySinh(txtsNgaySinh.getText());
        _tblThanhVienEO.setsNgayMat(txtsNgayMat.getText());
        _tblThanhVienEO.setsNoiAnTang(txtsNoiAnTang.getText());
        _tblThanhVienEO.setsTrinhDoVH(txtsTrinhDoVH.getText());
        _tblThanhVienEO.setsNgheNghiep(txtsNgheNghiep.getText());
        _tblThanhVienEO.setiDoiThu(Integer.parseInt(txtiDoiThu.getText()));
        _tblThanhVienEO.setsNhomMau(txtsNhomMau.getText());
        _tblThanhVienEO.setsGhiChu(txtsGhiChu.getText());
        Object itemBo = cbbBo.getSelectedItem();
        Object itemMe = cbbMe.getSelectedItem();
        _tblQuanHeEO.setFk_MaTV_Bo(((ComboItem)itemBo).getValue());
         _tblQuanHeEO.setFK_MaTV_Me(((ComboItem)itemMe).getValue());
        try {
            int dialogResult = JOptionPane.showConfirmDialog(this, "Bạn muốn sửa bản ghi ko?", "Gia hệ dòng họ",JOptionPane.YES_NO_OPTION);
            if(dialogResult==0){
                if(tblThanhVienDAO.ThanhVien_Update(_tblThanhVienEO, _tblQuanHeEO) == true){
                    JOptionPane.showMessageDialog(this, "Sửa thành công");
                }
                else{
                    JOptionPane.showMessageDialog(this, "Lỗi sửa dữ liệu");
                }
                loadTable();
            }
        }
        catch (SQLException | HeadlessException ex) {
            JOptionPane.showMessageDialog(this, "Lỗi sửa dữ liệu: "+ ex.getMessage());
        }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        _tblThanhVienEO.setPK_iMaTV(Integer.parseInt(txtPK_iMaTV.getText()));
        _tblQuanHeEO.setFk_MaTV_Bo(Integer.parseInt(txtPK_iMaTV.getText()));
        try {
            if(tblQuanHeDAO.QuanHe_CheckExist_Fk_MaTV_Bo(_tblQuanHeEO) == true){
                JOptionPane.showMessageDialog(this, "Không cho phép xóa. Người này đã có con");
            }
            else{
                int dialogResult = JOptionPane.showConfirmDialog(this, "Bạn muốn xóa bản ghi ko?", "Gia phả dòng họ",JOptionPane.YES_NO_OPTION);
                if(dialogResult==0){
                    if(tblThanhVienDAO.ThanhVien_Delete(_tblThanhVienEO) == true){
                        JOptionPane.showMessageDialog(this, "Xóa thành công");
                    }
                    else{
                        JOptionPane.showMessageDialog(this, "Lỗi xóa dữ liệu");
                    }
                    loadTable();  
                }
            }
        }
        catch (SQLException | HeadlessException ex) {
            JOptionPane.showMessageDialog(this, "Lỗi xóa dữ liệu: "+ ex.getMessage());
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed

        _tblThanhVienEO.setsTenTV(txtsTenTV.getText());
        _tblThanhVienEO.setsTenHuy(txtsTenHuy.getText());
        if(rbtnNam.isSelected()==true){
             _tblThanhVienEO.setbGioiTinh(true);
        }
        
        if(rbtnNu.isSelected()==true){
             _tblThanhVienEO.setbGioiTinh(false);
        }
        _tblThanhVienEO.setsNoiSinh(txtsNoiSinh.getText());
        _tblThanhVienEO.setsNoiOHienTai(txtsNoiOHienTai.getText());
        _tblThanhVienEO.setsNgaySinh(txtsNgaySinh.getText());
        _tblThanhVienEO.setsNgayMat(txtsNgayMat.getText());
        _tblThanhVienEO.setsNoiAnTang(txtsNoiAnTang.getText());
        _tblThanhVienEO.setsTrinhDoVH(txtsTrinhDoVH.getText());
        _tblThanhVienEO.setsNgheNghiep(txtsNgheNghiep.getText());
        _tblThanhVienEO.setiDoiThu(Integer.parseInt(txtiDoiThu.getText()) );
        _tblThanhVienEO.setsNhomMau(txtsNhomMau.getText());
        _tblThanhVienEO.setsGhiChu(txtsGhiChu.getText());
        Object itemBo = cbbBo.getSelectedItem();
        Object itemMe = cbbMe.getSelectedItem();
        _tblQuanHeEO.setFk_MaTV_Bo(((ComboItem)itemBo).getValue());
         _tblQuanHeEO.setFK_MaTV_Me(((ComboItem)itemMe).getValue());
         
        
        try {
            if(tblThanhVienDAO.ThanhVien_Insert(_tblThanhVienEO, _tblQuanHeEO) == true){
                JOptionPane.showMessageDialog(this, "Thêm thành công");
            }
            else{
                JOptionPane.showMessageDialog(this, "Lỗi thêm dữ liệu");
            }
            loadTable();
        }
        catch (SQLException | HeadlessException ex) {
            JOptionPane.showMessageDialog(this, "Lỗi thêm dữ liệu: "+ ex.getMessage());
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void rbtnNamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnNamActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rbtnNamActionPerformed

    private void rbtnNuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnNuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rbtnNuActionPerformed

    private void txtsGhiChuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtsGhiChuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtsGhiChuActionPerformed

    private void txtPK_iMaTVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPK_iMaTVActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPK_iMaTVActionPerformed

    private void tbThanhVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbThanhVienMouseClicked
        // TODO add your handling code here:
      int index = tbThanhVien.getSelectedRow();
      txtPK_iMaTV.setText(String.valueOf(tbThanhVien.getValueAt(index, 0)));
      tblThanhVienEO objtblThanhVienEO = new tblThanhVienEO(0, null, null, false,null, null, null, null, null, null, null, 0 , null, null);
      objtblThanhVienEO.setPK_iMaTV(Integer.parseInt(txtPK_iMaTV.getText()));
        try {
            ResultSet rs = tblThanhVienDAO.ThanhVien_SelectItem(objtblThanhVienEO);
            while (rs.next()) {
                txtsTenTV.setText(String.valueOf(rs.getString(2)));  // Mã TV kiểu String
                txtsTenHuy.setText(String.valueOf(rs.getString(3)));
                if(Boolean.valueOf(rs.getBoolean(4))==true)
                    rbtnNam.setSelected(true);
                 if(Boolean.valueOf(rs.getBoolean(4))==false)
                    rbtnNu.setSelected(true);
                txtsNoiSinh.setText(String.valueOf(rs.getString(5)));
                txtsNoiOHienTai.setText(String.valueOf(rs.getString(6)));
                txtsNgaySinh.setText(String.valueOf(rs.getString(7)));
                if(rs.getString(8) == null){
                txtsNgayMat.setText("");
                }
                else{
                txtsNgayMat.setText(String.valueOf(rs.getString(8)));
                }    
                if(rs.getString(9) == null){
                txtsNoiAnTang.setText("");
                }
                else{
                txtsNoiAnTang.setText(String.valueOf(rs.getString(9)));
                }                 
                txtsTrinhDoVH.setText(String.valueOf(rs.getString(10)));
                txtsNgheNghiep.setText(String.valueOf(rs.getString(11)));
                txtiDoiThu.setText(String.valueOf(rs.getString(12)));
                txtsNhomMau.setText(String.valueOf(rs.getString(13)));
                if(rs.getString(14) == null){
                txtsGhiChu.setText("");
                }
                else{
                txtsGhiChu.setText(String.valueOf(rs.getString(14)));
                } 
                ComboItem.setSelectedValue(cbbBo,rs.getInt(15));
                ComboItem.setSelectedValue(cbbMe,rs.getInt(16));
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "Lỗi Load Dữ liệu tại bảng Thành viên:' "+e.getMessage()+ " '");
        }
    }//GEN-LAST:event_tbThanhVienMouseClicked

    
    private void cbbBoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbBoActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_cbbBoActionPerformed

    private void txtsNoiSinhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtsNoiSinhActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtsNoiSinhActionPerformed

    private void txtsNoiOHienTaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtsNoiOHienTaiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtsNoiOHienTaiActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThanhVien_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ThanhVien_UI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JButton btnXoa;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox cbbBo;
    private javax.swing.JComboBox cbbMe;
    private datechooser.beans.DateChooserDialog dateChooserDialog1;
    private datechooser.beans.DateChooserDialog dateChooserDialog2;
    private datechooser.beans.DateChooserDialog dateChooserDialog3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbtnNam;
    private javax.swing.JRadioButton rbtnNu;
    private javax.swing.JTable tbThanhVien;
    private javax.swing.JTextField txtPK_iMaTV;
    private javax.swing.JTextField txtiDoiThu;
    private javax.swing.JTextField txtsGhiChu;
    private javax.swing.JTextField txtsNgayMat;
    private javax.swing.JTextField txtsNgaySinh;
    private javax.swing.JTextField txtsNgheNghiep;
    private javax.swing.JTextField txtsNhomMau;
    private javax.swing.JTextField txtsNoiAnTang;
    private javax.swing.JTextField txtsNoiOHienTai;
    private javax.swing.JTextField txtsNoiSinh;
    private javax.swing.JTextField txtsTenHuy;
    private javax.swing.JTextField txtsTenTV;
    private javax.swing.JTextField txtsTrinhDoVH;
    // End of variables declaration//GEN-END:variables
}
